

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Blue~u~u~u">
  <meta name="author" content="Blue~u~u~u">
  <meta name="keywords" content="">
  <meta name="description" content="Docker安装FastDfs和上传图片入门小程序 1.搭建FastDfs拉取镜像 1docker pull morunchang&#x2F;fastdfs  运行tracker   1docker run -d --name tracker --net&#x3D;host morunchang&#x2F;fastdfs sh tracker.sh   –net&#x3D;host 和虚拟机使用同一套网络  运行storage 1doc">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker安装FastDfs和上传图片入门小程序">
<meta property="og:url" content="http://www.slx.blue/2022/03/19/docker-fastdfs/index.html">
<meta property="og:site_name" content="Blue~u~u~u~u">
<meta property="og:description" content="Docker安装FastDfs和上传图片入门小程序 1.搭建FastDfs拉取镜像 1docker pull morunchang&#x2F;fastdfs  运行tracker   1docker run -d --name tracker --net&#x3D;host morunchang&#x2F;fastdfs sh tracker.sh   –net&#x3D;host 和虚拟机使用同一套网络  运行storage 1doc">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker000.png">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker001.png">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker002.png">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker003.png">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker005.png">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker006.png">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker007.png">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker008.png">
<meta property="article:published_time" content="2022-03-19T08:38:36.000Z">
<meta property="article:modified_time" content="2022-03-19T08:39:30.983Z">
<meta property="article:author" content="Blue~u~u~u">
<meta property="article:tag" content="docker">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker000.png">
  
  <title>Docker安装FastDfs和上传图片入门小程序 - Blue~u~u~u~u</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.slx.blue","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":180,"cursorChar":"_","loop":true},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>

<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"/>
<script src="/live2d-widget/autoload.js"></script>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Blue~u~u</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/images/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Docker安装FastDfs和上传图片入门小程序">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-03-19 16:38" pubdate>
        2022年3月19日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      5k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      15 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Docker安装FastDfs和上传图片入门小程序</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2 个月前
                
              </p>
            
            <div class="markdown-body">
              <h1 id="Docker安装FastDfs和上传图片入门小程序"><a href="#Docker安装FastDfs和上传图片入门小程序" class="headerlink" title="Docker安装FastDfs和上传图片入门小程序"></a>Docker安装FastDfs和上传图片入门小程序</h1><p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker000.png" srcset="/img/loading.gif" lazyload alt="image-20220319162748847"></p>
<h2 id="1-搭建FastDfs"><a href="#1-搭建FastDfs" class="headerlink" title="1.搭建FastDfs"></a>1.搭建FastDfs</h2><p>拉取镜像</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">docker</span> <span class="hljs-string">pull morunchang/fastdfs</span><br></code></pre></td></tr></table></figure>

<p>运行tracker  </p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">docker</span> <span class="hljs-string">run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh</span><br></code></pre></td></tr></table></figure>

<blockquote>
<p>–net=host 和虚拟机使用同一套网络</p>
</blockquote>
<p>运行storage</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">docker</span> <span class="hljs-string">run -d --name storage --net=host -e TRACKER_IP=服务器IP:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh</span><br></code></pre></td></tr></table></figure>

<ul>
<li>使用的网络模式是–net=host, 此时会将宿主机的网络应用于容器，链接容器就可以直接使用宿主机的IP</li>
<li><code>sh tracker.sh</code>运行tracker.sh脚本文件</li>
<li>group1是组名，即storage的组  </li>
<li>如果想要增加新的storage服务器，再次运行该命令，注意更换 新组名</li>
</ul>
<p><strong>配置Nginx</strong></p>
<p>Nginx在这里主要提供对FastDFS图片访问的支持，Docker容器中已经集成了Nginx，我们需要修改nginx的配置,进入storage的容器内部，修改nginx.conf</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">docker</span> <span class="hljs-string">exec -it storage  /bin/bash</span><br></code></pre></td></tr></table></figure>

<p>进入后</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">vi</span> <span class="hljs-string">/etc/nginx/conf/nginx.conf</span><br></code></pre></td></tr></table></figure>

<p>添加以下内容：</p>
<p>上图配置如下：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">location</span> <span class="hljs-string">~ /M00 &#123;</span><br>     <span class="hljs-attr">ngx_fastdfs_module;</span><br><span class="hljs-attr">&#125;</span><br></code></pre></td></tr></table></figure>

<p>访问图片的时候，浏览器通常都会对图片进行缓存，如果有禁止缓存，可以设置nginx配置添加禁止缓存即可。</p>
<p>禁止缓存：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">add_header</span> <span class="hljs-string">Cache-Control no-store;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker001.png" srcset="/img/loading.gif" lazyload alt="image-20220319155131530"></p>
<p>退出容器：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">exit</span><br></code></pre></td></tr></table></figure>

<p>重启storage容器：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">docker</span> <span class="hljs-string">restart storage</span><br></code></pre></td></tr></table></figure>

<p>查看启动容器<code>docker ps</code></p>
<p>开启启动设置：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">docker</span> <span class="hljs-string">update --restart=always tracker</span><br><span class="hljs-attr">docker</span> <span class="hljs-string">update --restart=always storage</span><br></code></pre></td></tr></table></figure>

<p>安装Nginx目的：</p>
<blockquote>
<p>nginx集成了FastDFS，可以通过它的ngx_fastdfs_module模块，可以通过该模块访问Tracker获取图片所存储的Storage信息，然后访问Storage信息获取图片信息。</p>
</blockquote>
<p>至此就搭建好啦！</p>
<h2 id="2-可能遇到的问题"><a href="#2-可能遇到的问题" class="headerlink" title="2.可能遇到的问题"></a>2.可能遇到的问题</h2><p>查看client.conf ，检查tracker_server地址</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs awk">vi  <span class="hljs-regexp">/etc/</span>fdfs/client.conf<br></code></pre></td></tr></table></figure>

<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker002.png" srcset="/img/loading.gif" lazyload alt="image-20220319155921796"></p>
<p>如果是云服务器，需要检查一下开放的端口号；</p>
<ul>
<li> tracker server的端口号 默认<strong>22122</strong> 在tracker.conf中</li>
<li>storage server的端口号 默认<strong>23000</strong> 在storage.conf中</li>
</ul>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs awk">vi <span class="hljs-regexp">/etc/</span>fdfs/storage.conf<br>vi <span class="hljs-regexp">/etc/</span>fdfs/tracker.conf<br></code></pre></td></tr></table></figure>

<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker003.png" srcset="/img/loading.gif" lazyload alt="image-20220319160301088"></p>
<p>此时需要防火墙开放这两个端口</p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker005.png" srcset="/img/loading.gif" lazyload alt="image-20220319160456004"></p>
<h2 id="3-小程序"><a href="#3-小程序" class="headerlink" title="3.小程序"></a>3.小程序</h2><p>搭好了那我们就写个小程序玩一玩吧</p>
<p>先上工具类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> org.csource.fastdfs.ClientGlobal;<br><span class="hljs-keyword">import</span> org.csource.fastdfs.StorageClient;<br><span class="hljs-keyword">import</span> org.csource.fastdfs.TrackerClient;<br><span class="hljs-keyword">import</span> org.csource.fastdfs.TrackerServer;<br><span class="hljs-keyword">import</span> org.springframework.core.io.ClassPathResource;<br><span class="hljs-keyword">import</span> org.springframework.util.StringUtils;<br><span class="hljs-keyword">import</span> org.springframework.web.multipart.MultipartFile;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@program</span>: gmall</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span>: 龙龙</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@create</span>: 2022-03-19 14:04</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@description</span>:</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FastDfsUtil</span> </span>&#123;<br>    <span class="hljs-comment">//静态代码块 只需要加载一次</span><br>    <span class="hljs-keyword">static</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">//加载classpath下的配置文件</span><br>            ClassPathResource resource = <span class="hljs-keyword">new</span> ClassPathResource(<span class="hljs-string">&quot;tracker.conf&quot;</span>);<br>            <span class="hljs-comment">//初始化加载tracker的信息</span><br>            ClientGlobal.init(resource.getPath());<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>            e.printStackTrace();<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 文件上传</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> multipartFile</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title">fileUpload</span><span class="hljs-params">(MultipartFile multipartFile)</span> <span class="hljs-keyword">throws</span> Exception </span>&#123;<br>        <span class="hljs-comment">//初始化TrackClient</span><br>        TrackerClient trackerClient = <span class="hljs-keyword">new</span> TrackerClient();<br>        <span class="hljs-comment">//获取连接</span><br>        TrackerServer trackerServer = trackerClient.getConnection();<br>        <span class="hljs-comment">//通过tracker获取storage的信息</span><br>        StorageClient storageClient = <span class="hljs-keyword">new</span> StorageClient(trackerServer, <span class="hljs-keyword">null</span>);<br>        <span class="hljs-comment">//通过storage完成文件的上传</span><br>        <span class="hljs-comment">/**</span><br><span class="hljs-comment">         * 1.文件的字节码</span><br><span class="hljs-comment">         * 2.文件的拓展名</span><br><span class="hljs-comment">         * 3.附加参数: 比如 时间 地点 水印 人等</span><br><span class="hljs-comment">         */</span><br>        String[] strings = storageClient.upload_file(<br>                multipartFile.getBytes(),  <span class="hljs-comment">//字节码</span><br>                StringUtils.getFilenameExtension(multipartFile.getOriginalFilename()), <span class="hljs-comment">//扩展名</span><br>                <span class="hljs-keyword">null</span>);<span class="hljs-comment">//元数据列表</span><br>		<span class="hljs-comment">// strings[0]： group1</span><br>        <span class="hljs-comment">// strings[1]： M00/00/00/CgAIA2I1kveAEYRLAABygkS2Cqw568.jpg</span><br>        <span class="hljs-keyword">return</span> strings[<span class="hljs-number">0</span>] + <span class="hljs-string">&quot;/&quot;</span> + strings[<span class="hljs-number">1</span>];<br><br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 文件的下载</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> groupName</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fileName</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">byte</span>[] downLoad(String groupName, String fileName) <span class="hljs-keyword">throws</span> Exception &#123;<br>        <span class="hljs-comment">//初始化trackerClient</span><br>        TrackerClient trackerClient = <span class="hljs-keyword">new</span> TrackerClient();<br>        <span class="hljs-comment">//获取连接</span><br>        TrackerServer trackerServer = trackerClient.getConnection();<br>        <span class="hljs-comment">//通过tracker获取storage的信息</span><br>        StorageClient storageClient = <span class="hljs-keyword">new</span> StorageClient(trackerServer, <span class="hljs-keyword">null</span>);<br>        <span class="hljs-comment">//通过storage完成文件的下载</span><br>        <span class="hljs-keyword">byte</span>[] bytes = storageClient.download_file(groupName, fileName);<br>        <span class="hljs-comment">//返回结果</span><br>        <span class="hljs-keyword">return</span> bytes;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 文件的删除</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Boolean <span class="hljs-title">deleteFile</span><span class="hljs-params">(String groupName, String fileName)</span> <span class="hljs-keyword">throws</span> Exception </span>&#123;<br>        <span class="hljs-comment">//初始化trackerClient</span><br>        TrackerClient trackerClient = <span class="hljs-keyword">new</span> TrackerClient();<br>        <span class="hljs-comment">//获取连接</span><br>        TrackerServer trackerServer = trackerClient.getConnection();<br>        <span class="hljs-comment">//通过tracker获取storage的信息</span><br>        StorageClient storageClient = <span class="hljs-keyword">new</span> StorageClient(trackerServer, <span class="hljs-keyword">null</span>);<br>        <span class="hljs-comment">//通过storage完成文件的文件的删除</span><br>        <span class="hljs-keyword">int</span> i = storageClient.delete_file(groupName, fileName);<br>        <span class="hljs-comment">//返回结果</span><br>        <span class="hljs-keyword">return</span> i &gt;= <span class="hljs-number">0</span> ? <span class="hljs-keyword">true</span> : <span class="hljs-keyword">false</span>;<br>    &#125;<br><br>&#125;<br><br></code></pre></td></tr></table></figure>

<p>在resources下创建tracker.conf文件</p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker006.png" srcset="/img/loading.gif" lazyload alt="image-20220319161108385"></p>
<p>配置如下：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">tracker_server</span>=<span class="hljs-string">你的服务器IP:22122</span><br><span class="hljs-comment"># 连接超时时间，针对socket套接字函数connect，默认为30秒</span><br><span class="hljs-attr">connect_timeout</span>=<span class="hljs-string">30000</span><br><span class="hljs-comment"># 网络通讯超时时间，默认是60秒</span><br><span class="hljs-attr">network_timeout</span>=<span class="hljs-string">60000</span><br></code></pre></td></tr></table></figure>

<p>Controller代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> slx.blue.gmall.product.controller;<br><br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.PostMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestParam;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RestController;<br><span class="hljs-keyword">import</span> org.springframework.web.multipart.MultipartFile;<br><span class="hljs-keyword">import</span> slx.blue.gmall.common.result.Result;<br><span class="hljs-keyword">import</span> slx.blue.gmall.product.util.FastDfsUtil;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@program</span>: gmall</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span>: 龙龙</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@create</span>: 2022-03-19 12:52</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@description</span>: 文件上传控制器</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-meta">@RestController</span><br><span class="hljs-meta">@RequestMapping(&quot;/admin/product&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FileController</span> </span>&#123;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;fileServer.url&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> String imageUrl; <span class="hljs-comment">//http://你的IP地址:8080/  这里放在yml文件中</span><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">    	fileServer:</span><br><span class="hljs-comment">  			url: http://你的IP地址:8080/ # fastDFS 分布式文件系统的 ip:port</span><br><span class="hljs-comment">    */</span><br><br>    <span class="hljs-meta">@PostMapping(&quot;/fileUpload&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Result <span class="hljs-title">fileUpload</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam(&quot;file&quot;)</span> MultipartFile multipartFile)</span> <span class="hljs-keyword">throws</span> Exception </span>&#123;<br>        String fileName = FastDfsUtil.fileUpload(multipartFile);<br>        <span class="hljs-comment">//返回可直接访问的图片地址 url + fileName</span><br>        <span class="hljs-keyword">return</span> Result.ok(imageUrl + fileName);<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p>跑起来后我们用postman测试一下</p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker007.png" srcset="/img/loading.gif" lazyload alt="image-20220319161627945"></p>
<p>拿到地址访问成功！</p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/docker/FastDfs/Docker008.png" srcset="/img/loading.gif" lazyload alt="image-20220319161953101"></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/docker/">docker</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/docker/">docker</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客目前大部分文章都是参考尚硅谷或者马士兵教育的学习资料！<a href="http://www.atguigu.com/" rel="nofollow noopener"官网地址！</a> 
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/03/23/redis-%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">redis-分布式锁</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/03/15/MyCat/">
                        <span class="hidden-mobile">MyCat入门配置详解和常见九种数据分片算法</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">

  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
	<!--《添加网站运行时间 -->
<br/>

<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date(); 

function createtime() {
    //此处修改你的建站时间或者网站上线时间
    var grt = new Date('11/02/2021 21:39:00');
    now.setTime(now.getTime() + 250);
    days = (now - grt) / 1000 / 60 / 60 / 24;

    dnum = Math.floor(days);
    hours = (now - grt) / 1000 / 60 / 60 - (24 * dnum);
    hnum = Math.floor(hours);
    if (String(hnum).length == 1) {
        hnum = "0" + hnum;
    }
    minutes = (now - grt) / 1000 / 60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes);
    if (String(mnum).length == 1) {
        mnum = "0" + mnum;
    }
    seconds = (now - grt) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds);
    if (String(snum).length == 1) {
        snum = "0" + snum;
    }
    document.getElementById("timeDate").innerHTML = " 本站已各种夹缝中安全运行 " + dnum + " 天 ";
    document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()", 250);
</script>

<!-- 添加网站运行时间》-->
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
